package com.IntelligentCommunity.community.service.impl;

import com.IntelligentCommunity.algorithm.api.AlgorithmRemote;
import com.IntelligentCommunity.algorithm.domain.request.FaceAddRequest;
import com.IntelligentCommunity.community.domain.*;
import com.IntelligentCommunity.community.mapper.RealPopulationMapper;
import com.IntelligentCommunity.community.service.IRealPopulationService;
import com.alibaba.cloud.commons.lang.StringUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 实有人口统计Service业务层处理
 *
 * @author zzpjx
 * @date 2024-07-19
 */
@Service
@Log4j2
public class RealPopulationServiceImpl implements IRealPopulationService
{
    @Autowired
    private AlgorithmRemote algorithmRemote;

    @Autowired
    private RealPopulationMapper realPopulationMapper;

    /**
     * 查询实有人口统计
     *
     * @param id 实有人口统计主键
     * @return 实有人口统计
     */
    @Override
    public RealPopulation selectRealPopulationById(Long id){
        return realPopulationMapper.selectRealPopulationById(id);
    }

    /**
     * 查询实有人口统计列表
     *
     * @param realPopulation 实有人口统计
     * @return 实有人口统计
     */
    @Override
    public List<RealPopulation> selectRealPopulationList(RealPopulation realPopulation){
        List<RealPopulation> realPopulations = realPopulationMapper.selectRealPopulationList(realPopulation);
        return realPopulations;
    }

    /**
     * 新增实有人口统计
     *
     * @param realPopulation 实有人口统计
     * @return 结果
     */
    @Override
    public int insertRealPopulation(RealPopulation realPopulation){
        int i = realPopulationMapper.insertRealPopulation(realPopulation);
        Long[] labelIds = realPopulation.getLabelIds();
        if(labelIds!=null){
            realPopulationMapper.insertRealPopulationLableList(realPopulation.getId(),labelIds);
        }
        realPopulationMapper.insertRealPopulationAndElement(realPopulation.getId(),realPopulation.getLabelId());
        if (StringUtils.isNotBlank(realPopulation.getPicture())){

            FaceAddRequest faceAddRequest = new FaceAddRequest();
            faceAddRequest.setSubject("person_id," + realPopulation.getId());
            faceAddRequest.setImg(realPopulation.getPicture());
            faceAddRequest.setFlag(0);
            algorithmRemote.faceInsert(faceAddRequest);
        }
        return i;
    }

    /**
     * 修改实有人口统计
     *
     * @param realPopulation 实有人口统计
     * @return 结果
     */
    @Override
    @Transactional
    public int updateRealPopulation(RealPopulation realPopulation) {
        try {
            int i = realPopulationMapper.updateRealPopulation(realPopulation);
            updateRealPopulationLabels(realPopulation.getId(), realPopulation.getLabelIds());

            RealPopulation population = realPopulationMapper.selectRealPopulationAndElement(realPopulation.getId());
            if(population!=null){
                realPopulationMapper.updateRealPopulationAndElement(realPopulation.getId(), realPopulation.getLabelId());
            }
            if (population == null) {
                realPopulationMapper.insertRealPopulationAndElement(realPopulation.getId(), realPopulation.getLabelId());
            }
            return i;
        } catch (Exception e) {
            throw new RuntimeException("更新实际人口信息失败: " + e.getMessage(), e);
        }
    }

    private void updateRealPopulationLabels(Long realPopulationId, Long[] labelIds) {
        realPopulationMapper.deleteRealPopulationLableList(realPopulationId);
        if (labelIds != null && !(labelIds.length == 0)) {
            realPopulationMapper.insertRealPopulationLableList(realPopulationId, labelIds);
        }
    }


    /**
     * 批量删除实有人口统计
     *
     * @param ids 需要删除的实有人口统计主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteRealPopulationByIds(Long[] ids)
    {
        int i = realPopulationMapper.deleteRealPopulationByIds(ids);
        realPopulationMapper.deleterealPersonHouseholdRelations(ids);
        return i;
    }

    /**
     * 删除实有人口统计信息
     *
     * @param id 实有人口统计主键
     * @return 结果
     */
    @Override
    public int deleteRealPopulationById(Long id)
    {
        return realPopulationMapper.deleteRealPopulationById(id);
    }

    @Override
    public void syncFace2Algorithm() {
        List<FaceAddRequest> face2Algorithm = realPopulationMapper.syncFace2Algorithm();

        for (FaceAddRequest faceAddRequest : face2Algorithm) {
            algorithmRemote.faceInsert(faceAddRequest);
        }

    }

    /**
     * 功能：实有车辆户主下拉框查询
     * @return
     */
    @Override
    public List<RealPopulation> populationList() {
        List<RealPopulation> list = realPopulationMapper.populationList();
        return list;
    }

    @Override
    public void insertRealPopulationList(List<RealPopulation> list) {
        realPopulationMapper.insertRealPopulationList(list);
    }

    @Override
    public void updateTimeById(Long id) {
        realPopulationMapper.updateTimeById(id);
    }

    /**
     * 功能：查询人员类型下拉框查询
     * @return
     */
    @Override
    public List<PopulationType> populationType() {
        List<PopulationType> list = realPopulationMapper.populationType();
        return list;
    }

    /**
     * 功能：查询人员标签
     * @return
     */
    @Override
    public List<RealLabel> labelList() {
        List<RealLabel> list = realPopulationMapper.labelList();
        return list;
    }

    @Override
    public List<RealEstate> selectRidgepole(Integer id) {
        return realPopulationMapper.selectRidgepole(id);
    }

    @Override
    public List<RealEstate> selectElement(Integer id, Integer did) {
        return realPopulationMapper.selectElement(id,did);
    }

    @Override
    public List<RealEstate> selectHouseholds(Integer element, Integer ridgepole, Integer did) {
        return realPopulationMapper.selectHouseholds(element,ridgepole,did);
    }

    /**
     * 功能：查询当前小区下的实有人口
     * @param districtId
     * @return
     */
    @Override
    public List<RealPopulation> selPopulation(Integer districtId) {
        List<RealPopulation> list = realPopulationMapper.selPopulation(districtId);
        return list;
    }

    @Override
    @Transactional
    public void handleEmigration(Emigration emigration) {
        if (emigration.getId() == null) {
            throw new IllegalArgumentException("迁出用户ID不能为空");
        }
        realPopulationMapper.handleEmigration(emigration);
        realPopulationMapper.updateStata(emigration.getId());
    }

    @Override
    public List<Emigration> selectEmigrationLog(Integer id) {
        return realPopulationMapper.selectEmigrationLog(id);
    }


}
